package com.baoer.service.impl;

import com.baoer.R;
import com.baoer.dto.req.SalesDetailReqDTO;
import com.baoer.dto.req.SalesStatisticsReqDTO;
import com.baoer.dto.resp.*;
import com.baoer.dto.resp.OrderInfoRespDTO;
import com.baoer.mapper.OrderMapper;
import com.baoer.dto.req.OrderInfoReqDTO;
import com.baoer.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: YING
 * @CreateTime: 2025-06-26
 * @Description:
 * @Version: 1.0
 */

@Service
@RequiredArgsConstructor
public class OrderServiceImpl implements OrderService {
    private final OrderMapper orderMapper;
    @Override
    public R getOrderInfo(OrderInfoReqDTO orderInfoReqDTO) {
        Integer pageIndex = orderInfoReqDTO.getPageIndex();
        Integer pageSize = orderInfoReqDTO.getPageSize();
        pageIndex = (pageIndex - 1) * pageSize;
        Double pageCount = orderMapper.OrderPageCount();
        pageCount = Math.ceil(pageCount / pageSize);
        orderInfoReqDTO.setPageIndex(pageIndex);
        List<OrderInfoRespDTO> orderInfo = orderMapper.getOrderInfo(orderInfoReqDTO);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("pageCount", pageCount);
        map.put("orderInfo", orderInfo);
        return R.ok(map);
    }

    @Override
    public R getOrderdetail(Integer id) {
        OrderdetailRespDTO orderdetail = orderMapper.getOrderdetail(id);
        return R.ok(orderdetail) ;
    }

    @Override
    public R getSalesStatistics(SalesStatisticsReqDTO salesStatisticsReqDTO) {
        Integer pageIndex = salesStatisticsReqDTO.getPageIndex();
        Integer pageSize = salesStatisticsReqDTO.getPageSize();
        pageIndex = (pageIndex - 1) * pageSize;
        Double pageCount = orderMapper.SalesStatisticsPageCount();
        pageCount = Math.ceil(pageCount / pageSize);
        salesStatisticsReqDTO.setPageIndex(pageIndex);
        List<SalesStatisticsRespDTO> salesStatisticsRespDTO=orderMapper.getSalesStatistics(salesStatisticsReqDTO);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("pageCount", pageCount);
        map.put("salesStatisticsRespDTO", salesStatisticsRespDTO);
        return R.ok(map);
    }

    @Override
    public R getBranchStatistics(SalesStatisticsReqDTO salesStatisticsReqDTO) {
        Integer pageIndex = salesStatisticsReqDTO.getPageIndex();
        Integer pageSize = salesStatisticsReqDTO.getPageSize();
        pageIndex = (pageIndex - 1) * pageSize;
        Double pageCount = orderMapper.BranchStatisticsPageCount();
        pageCount = Math.ceil(pageCount / pageSize);
        salesStatisticsReqDTO.setPageIndex(pageIndex);
        List<SalesStatisticsRespDTO> salesStatisticsRespDTO=orderMapper.getBranchStatistics(salesStatisticsReqDTO);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("pageCount", pageCount);
        map.put("salesStatisticsRespDTO", salesStatisticsRespDTO);
        return R.ok(map);
    }

    @Override
    public R getSalesDetail(SalesDetailReqDTO salesDetailReqDTO) {
        Integer pageIndex = salesDetailReqDTO.getPageIndex();
        Integer pageSize = salesDetailReqDTO.getPageSize();
        pageIndex = (pageIndex - 1) * pageSize;
        salesDetailReqDTO.setPageIndex(pageIndex);
        SalesDetailRespDTO salesDetail=orderMapper.getSalesDetail(salesDetailReqDTO);
        return R.ok(salesDetail);
    }
}
